package com.lihh;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

public class DistributedLockTest {
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    final DistributedLock lock1 = new DistributedLock();
    final DistributedLock lock2 = new DistributedLock();
    
    new Thread(new Runnable() {
      @Override
      public void run() {
        
        try {
          lock1.lock();
          System.out.println("线程1 启动 获取到锁");
          
          Thread.sleep(15 * 1000);
          lock1.unLock();
          System.out.println("线程1 释放锁");
        } catch (InterruptedException | KeeperException e) {
          e.printStackTrace();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }).start();
    
    new Thread(new Runnable() {
      @Override
      public void run() {
        
        try {
          lock2.lock();
          System.out.println("线程2 启动 获取到锁");
          
          Thread.sleep(15 * 1000);
          lock2.unLock();
          System.out.println("线程2 释放锁");
        } catch (InterruptedException | KeeperException e) {
          e.printStackTrace();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }).start();
  }
}
